package com.greenline.push.common.cookie;

import javax.servlet.http.HttpServletRequest;

import com.greenline.framework.mursi.web.module.cookie.CookieModule;

/**
 * @Type ValidCodeCookieUtil
 * @Desc 验证码cookie工具类
 * @author weirui.shenwr
 * @date 2012-3-21
 * @Version V1.0
 */
public class ValidCodeCookieUtil extends CookieUtil {

    /** 用户验证码 */
    private static final String VALID_CODE = "validCode";

    /**
     * 设置验证码到cookie
     * 
     * @param request
     * @param user
     */
    public static void write(HttpServletRequest request, String code) {
        CookieModule jar = getCookieModule(request);
        if (jar == null) {
            throw new NullPointerException();
        }
        // 验证码
        jar.set(VALID_CODE, code);
    }

    /**
     * 清除cookie里的验证码信息
     * 
     * @param request
     */
    public static void clear(HttpServletRequest request) {
        CookieModule jar = getCookieModule(request);
        if (jar != null) {
            jar.remove(VALID_CODE);
        }
    }

    /**
     * 获取cookie里的验证码
     * 
     * @param request
     * @param key
     * @return
     */
    public static String getValidCode(HttpServletRequest request) {
        return getCookieValue(request, VALID_CODE);
    }

}
